package org.jeecg.modules.compilation.util;


import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

//Aspose-word破解  https://blog.csdn.net/kanglong129/article/details/103033450
@Slf4j
public class AsposeUtil {

//    public static String ToDoc(String input) throws Exception {
//
//        String output = input.replace("docx","doc");
//        Document doc = new Document(input);
//        doc.save(output);
//        return output;
//    }

    private static boolean getLicense() {
        boolean result = false;
        try {
            String osName = System.getProperty("os.name").toLowerCase();
            InputStream is;
            if (osName.contains("win")) {
                is =  new FileInputStream("E:\\license.xml");
            } else {
                log.info("获取签名路径：{}",Constants.CURRENT_DIR_JAR + Constants.CURRENT_SEPARATOR + "license.xml");
                is =  new FileInputStream("/root/license.xml");
            }

            License aposeLic = new License();
            aposeLic.setLicense(is);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    public static String ToDoc(String wordPath) {
        String output = wordPath.replace("docx","doc");
        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            return null;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(output);
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(wordPath);
            doc.save(os, SaveFormat.DOC);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒"); //转化用时
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("转换文件路径：{}",output);
        return output;
    }

    public static void ToPDF(String input,String output) {

        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
            log.info("License验证出错");
            return ;
        }
        try {
            long old = System.currentTimeMillis();
            File file = new File(output);
            FileOutputStream os = new FileOutputStream(file);
            Document doc = new Document(input);
            doc.save(os, SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
            long now = System.currentTimeMillis();
            os.close();
            log.info("共耗时：{}秒", ((now - old) / 1000.0));
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("转换文件路径：{}",output);
    }


//    public static void main(String [] arg) throws Exception {
//        // 验证License 若不验证则转化出的pdf文档会有水印产生
////        if (!getLicense()) {
////           System.out.println("no");
////        }
//        ToDoc("D:\\烟草制度项目（未结项）\\烟草局制度汇编环境部署\\document\\111.中烟销〔2021〕9号（广东烟草中山市有限责任公司关于印发《广东烟草中山市有限责任公司20支全零售连锁网络建设实施意见（修订）》及相关配套文件的通知）.docx");
////        Document doc = new Document("D:\\烟草制度项目（未结项）\\烟草局制度汇编环境部署\\document\\111.中烟销〔2021〕9号（广东烟草中山市有限责任公司关于印发《广东烟草中山市有限责任公司20支全零售连锁网络建设实施意见（修订）》及相关配套文件的通知）.docx");
////        doc.save("Output.doc");
//    }
}